<?php
	
	// University of Dayton SAE LDAP Login Script
	// Designed February 27, 2008
	// By Jared Szechy
	
	require('../common.php');
	require('ldap.php');
	
	$server = "ldap.udayton.edu";
	
	$username = $_POST["username"];
	$password = $_POST["password"];
	
	// The first step is to obtain the dn for the user
	// This is done through the anonymous access
	
	$ldapconn = ldap_connect($server);
	
	ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
	
	// Make the anonymous binding
	$ldapbind = ldap_bind($ldapconn);
	
	// Find the username
	$sr=ldap_search($ldapconn, "ou=Users,o=UofD", "cn=" . $username);
	$info = ldap_get_entries($ldapconn, $sr);
	
	// Obtain the dn
	$dn = $info[0]["dn"];
	
	ldap_close($ldapconn);
	
	// Make a new connection
	$ds=ldap_connect($server);
	
	// Make the binding using the dn obtained above and user's password
    $r=ldap_bind($ds, $dn, $password);

	// Find the user's entry
    $sr=ldap_search($ds, "ou=Users,o=UofD", "cn=" . $username);  
	
	$info = ldap_get_entries($ds, $sr);
	
	// If a student id number is obtained then the login was successful
	if( $info[0]['employeenumber'][0])
	{
		updateDB($info);
		$user = User::getByUsername($username);
		header("location: ../index.php");
	}
	else
	{
		echo "Invalid Login";
		setcookie('error', "Invalid username/password.");
		header("location: ../index.php");
	}
	
	ldap_close($ds);
		
?>